package com.fanghuaman.checkcode.controller;

import com.fanghuaman.checkcode.model.CheckCodeParamsDto;
import com.fanghuaman.checkcode.model.CheckCodeResultDto;
import com.fanghuaman.checkcode.service.CheckCodeService;
import com.fanghuaman.model.common.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shiminghui
 * @date 2024/12/22 17:02
 * @description: TODO
 */
@RestController
public class CheckCodeController {

    @Resource(name = "PicCheckCodeService")
    private CheckCodeService checkCodeService;

    /**
     * 生成图片验证码
     *
     * @param checkCodeParamsDto
     * @return
     */
    @PostMapping("/pic")
    public ResponseResult generatePicCheckCode(@RequestBody CheckCodeParamsDto checkCodeParamsDto) {
        CheckCodeResultDto generate = checkCodeService.generate(checkCodeParamsDto);
        return ResponseResult.success(generate);
    }

    /**
     * 校验
     *
     * @param key
     * @param code
     * @return
     */
    @PostMapping("/verify")
    public ResponseResult verify(String key, String code) {
        boolean verify = checkCodeService.verify(key, code);
        if (verify) {
            return ResponseResult.success(verify);
        } else {
            return ResponseResult.fail("校验码错误");
        }
    }
}
